package com.sandasai.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.sandasai.base.BaseDaoImpl;
import com.sandasai.dao.UserDao;
import com.sandasai.domain.User;
@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public User findByUsername(String username) {
		if(username == null) {
			return null;
		}else {
			@SuppressWarnings({ "rawtypes" })
			List list = getSession().createQuery("from User where username = :username").setString("username", username).list();
			if(list.size() == 1) {
				return (User)list.get(0);
			}else {
				return null;
			}
		}
	}

	@Override
	public User findByEmail(String email) {
		if(email == null) {
			return null;
		}else {
			@SuppressWarnings({ "rawtypes" })
			List list = getSession().createQuery("from User where email = :email").setString("email", email).list();
			if(list.size() == 1) {
				return (User)list.get(0);
			}else {
				return null;
			}
		}
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public User findByUsernameOrEmailAndPassword(String usernameOrEmail,
			String password) {
		if(usernameOrEmail == null || password == null) {
			return null;
		}else {
			String queryString = "from User where (username = :usernameOrEmail or email = :usernameOrEmail) and password = :password";
			List list = getSession().createQuery(queryString).setString("usernameOrEmail", usernameOrEmail).setString("password", password).list();
			if(list.size() == 1) {
				return (User)list.get(0);
			}else {
				return null;
			}
		}
	}
}
